package com.steven.springcloud2orderserviceribbon.controller;

import com.steven.springcloud2orderserviceribbon.config.RestTemplateConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author CXQ
 * @version 1.0
 */
@RestController
@RequestMapping("api/v1/order")
public class OrderController {

    private RestTemplate restTemplate;

    @Autowired
    public OrderController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @RequestMapping("insert")
    public Map<String, Object> insert(@RequestParam("product-id") String productId){

        String url = "http://127.0.0.1:8770/api/v1/product/select-by-id?id=" + productId;
        String product = restTemplate.getForObject(url, String.class);

        Map<String, Object> order = new HashMap<>(3);
        order.put("订单号", UUID.randomUUID().toString());
        order.put("订单时间", new Date());
        order.put("商品", product);

        return order;
    }

}
